package com.qd.panda.dc.dto;

import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.util.List;
import java.util.Optional;

/**
 * @author sjk
 */
@Data
@Accessors(chain = true)
@ToString
public class DcPage<T> {

    private List<T> data;
    private Integer pageNo;
    private Integer pageSize;
    private Long total;

    /**
     * 是否存在下一页
     *
     * @return true / false
     */
    public boolean hasNext() {
        return this.pageNo < this.getPages();
    }

    /**
     * 当前分页总页数
     */
    long getPages() {
        if (pageSize == 0) {
            return 0L;
        }
        long pages = getTotal() / pageSize;
        if (getTotal() % pageSize != 0) {
            pages++;
        }
        return pages;
    }

    public void freeData() {
        Optional.ofNullable(data).ifPresent(List::clear);
        this.pageNo = null;
        this.pageSize = null;
        this.total = null;
    }
}